package com.gcb.controller.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @Author 35235
 * @Date 2021/8/8 18:05
 * @Version 1.0
 */

public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("登录拦截器开");
       // System.out.println("request.getRequestURI() = " + request.getRequestURI());
        if (request.getRequestURI().contains("login")) {
            return true;
        }
        if (("/").equals(request.getRequestURI())){
//            request.getRequestDispatcher("/pages/login.html");
            response.sendRedirect("/pages/login.html");
            return true;
        }
        HttpSession session = request.getSession();
        if (session.getAttribute("username") != null) {
            return true;
        }

         request.getRequestDispatcher("/pages/login.html");
        System.out.println("跳转登录");
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("处理结束");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("清理完成");
    }

}
